<%@ include file="shared_header.jsp" %>
<%@ page import="vrp.Partner" %>
<%@ page import="vrp.Product" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
	
    query = "select from " + Partner.class.getName() + " where type==0 order by name asc";
    List<Partner> partners = (List<Partner>) pm.newQuery(query).execute();

    String error = request.getParameter("error");
    if (error != null) {
    	if (error.equals("inuse")) {
%>
    		<div class="ui-state-error">The operation cannot be done because the product has been placed in some order.</div>
<%
    	}
    }

    List<Product> products;	
	
	String partnerIDStr = request.getParameter("client");
	long partnerID = 0;
	if (partnerIDStr != null && !partnerIDStr.equals("") && !partnerIDStr.equals("0")) {
		partnerID = Integer.parseInt(partnerIDStr);
		Key partnerKey = KeyFactory.createKey(Partner.class.getSimpleName(), partnerID);
		query = "select from " + Product.class.getName() + " where partner == partnerParam parameters String partnerParam order by name asc";
		products = (List<Product>) pm.newQuery(query).execute(partnerKey);
	} else {
	    query = "select from " + Product.class.getName() + " order by name asc";
	    products = (List<Product>) pm.newQuery(query).execute();
	}

%>
	<div style="padding: 0px 10px;">
		<h2>List of Products</h2>
		<div>
			<form target="_self" action="/products.jsp">
				Client:&nbsp;
    			<select name="client">
    				<option value="">All Clients</option>
<%
    				for (Partner p : partners) {
%>			    			
				 	<option value="<%= p.getKey().getId() %>" <%= ((partnerID == p.getKey().getId())?"selected":"") %>><%= p.getName() %></option>
<%
       			}
%>							
				</select>
				<input type="Submit" value="Select" />
	    	</form>
		</div>		
		<br/>	
		<table style="width: 100%;">
<%
	
    if (products.isEmpty()) {
%>
			<tr><td>There is no product in the database.</td></tr>
<%
    } else {
%>
			<tr>
				<th>Name</th>
				<th>Price</th>
				<th>Weight</th>
				<th>Inventory</th>
				<th>Action</th>
			</tr>
<%
        for (Product p : products) {
%>
			<tr>
				<td style="text-align: left;"><%= p.getName() %></td>
				<td style="text-align: right;"><%= p.getPrice() %></td>
				<td style="text-align: right;"><%= p.getWeight() %></td>
				<td style="text-align: right;"><%= p.getInventory() %></td>
				<td style="text-align:center;">
					<input type="button" value="Edit" class="buttonedit"/>
					<input type="button" value="Delete" class="buttondelete"/>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="5">
					<form action="/editproduct" method="post">
						<div><b>Edit Product</b></div>
			    		<div>Name:&nbsp;<input type="text" name="name" value="<%= p.getName() %>" /></div>
			    		<div>Price:&nbsp;<input type="text" name="price" value="<%= p.getPrice() %>" /></div>
			    		<div>Weight:&nbsp;<input type="text" name="weight" value="<%= p.getWeight() %>" /></div>
			    		<div>Inventory:&nbsp;<input type="text" name="inventory" value="<%= p.getInventory() %>" /></div>
			    		<input type="hidden" name="key" value="<%= p.getKey().getId() %>"/>
						<input type="submit" value="Edit" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="5">
					<form action="/deleteproduct" method="post">
						<div><b>Delete Product</b></div>
			    		<div>This action would delete the above product permanently. Proceed?</div>
						<input type="hidden" name="key" value="<%= p.getKey().getId() %>"/>
						<input type="submit" value="Delete" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
<%
        }
    }
%>
			
		</table>
		<input type="button" value="Add Product" class="buttonadd" />
  		<div style="display: none;">
			<form action="/addproduct" method="post">
				<div>Client:&nbsp;
	    			<select name="partner">
<%
     				for (Partner p : partners) {
%>			    			
					 	<option value="<%= p.getKey().getId() %>" <%= ((partnerID == p.getKey().getId())?"selected":"") %>><%= p.getName() %></option>
<%
        			}
%>							
					</select>
	    		</div>
	    		<div>Name:&nbsp;<input type="text" name="name" /></div>
	    		<div>Price:&nbsp;<input type="text" name="price" /></div>
	    		<div>Weight:&nbsp;<input type="text" name="weight" /></div>
	    		<div>Inventory:&nbsp;<input type="text" name="inventory" /></div>
	    		<div><input type="submit" value="Add" /><input type="button" value="Cancel" class="buttoncancel" /></div>
	  		</form>
	  	</div>
	</div>
	
<%
    pm.close();
%>

<%@ include file="shared_footer.jsp" %>